<?php

/* 配送方式 shipping */
class ShippingModel extends BaseModel
{
	var $table  = 'shipping';
	var $prikey = 'shipping_id';
	var $_name  = 'shipping';
	var $_autov = array(
	'shipping_name' =>  array(
	'required'  => true,
	'filter'    => 'trim',
	),
	'cod_regions'   =>  array(
	'filter'    => 'trim',
	),
	'enabled'       =>  array(
	'filter'    => 'intval',
	),
	'sort_order'    =>  array(
	'filter'    => 'intval'
	),
	);

	var $_relation  =   array(
	// 一个配送方式只能属于一个店铺
	'belongs_to_store' => array(
	'model'         => 'store',
	'type'          => BELONGS_TO,
	'foreign_key'   => 'store_id',
	'reverse'       => 'has_shipping',
	),
	);

	function get_shipping_list($store_id)
	{
		$store_id = intval($store_id);
		$sql = "SELECT * from " . DB_PREFIX ."shipping  where store_id = $store_id ORDER BY sort_order DESC limit 50";
		$row = $this->db->getAll($sql);
		foreach ($row as $key=>$val)
		{
			$shipping_id = $val['shipping_id'];
			$area_sql = "SELECT * from " . DB_PREFIX ."shipping_area  where shipping_id = $shipping_id order by shipping_area_id asc";
			$res = $this->db->getAll($area_sql);
			$kuaidi = 0;
			foreach ($res as $k=>$v)
			{
				//if($v['shipping_way']==1)
				$kuaidi++;
				/*$configure = unserialize($v['configure']);
				if(is_array($configure))
				{
				foreach ($configure AS $rs)
				{
				$res[$k][$rs['name']] = $rs['value'];
				}
				}

				unset($res[$k]['configure']);
				unset($res[$k]['shipping_cfg']);*/
			}

			$row[$key]['area_shipping'] = $res;
			$row[$key]['kuaidi'] = $kuaidi;


		}
		return $row;
	}



	/**
 * 计算运费
 * @param   string  $shipping_id        配送方式ID
 * @param   float   $goods_weight       商品重量
 * @param   float   $goods_amount       商品金额
 * @param   float   $goods_number       商品数量
 * @param   float   $region_id          地区ID
 * @return  float   array
 */
	function shipping_fee($shipping_id, $goods_weight, $goods_amount, $goods_number,$region_id)
	{
		$region_id     = intval($region_id);
		$shipping_id  = intval($shipping_id);
		if($shipping_id==0)//免运费
		{
			$shipping['shipping_fee']  = 0;//运费
			$shipping['shipping_desc'] = '卖家承担运费';//运费详情

			return $shipping;
		}

		$region_mod = &m('region');
		$region = $region_mod->get_parents($region_id);//按地区ID，获得所有父级
		sort($region);//对健值重新排序
		$country = $region[0];//国家
		$provice = $region[1];//省份
		$city    = $region[2];//城市


		//默认全国地区
		$sql = "select s.compute_mode,a.region_id,a.shipping_id,s.shipping_name,a.item,a.item_price,a.step,a.step_price from " . DB_PREFIX ."shipping s left join " . DB_PREFIX ."shipping_area a
	  on s.shipping_id = a.shipping_id where a.shipping_id = $shipping_id and a.region_id = 1";

		$row = $this->db->getRow($sql);
		//免费额度，以后拓展用
		/*if ($row['free_money'] > 0 && $goods_amount >= $row['free_money'])
		{
		$shipping['shipping_fee']  == 0;//运费
		$shipping['shipping_desc'] == '订单满'.$row[free_money].'元免运费';//运费详情
		//return $shipping;
		}*/


		$compute_mode = !empty($row['compute_mode']) ? $row['compute_mode'] : 'by_weight';//按重量算还是按件算
		if ($compute_mode == 'by_weight')
		{
			$shipping_desc= '首费'.$row['item'].'公斤'.$row['item_price'].'元；每增加'.$row['step'].'公斤，增加运费'.$row['step_price'].'元';
			$unit   =  $goods_weight;//计算运费的单位为重量
		}
		else
		{
			$shipping_desc= '首费'.$row['item'].'件'.$row['item_price'].'元；每增加'.$row['step'].'件，增加运费'.$row['step_price'].'元';
			$unit  = $goods_number;//计算运费的单位为数量
		}

		$shipping_fee = $row['item_price'];//首费
		if ($unit > $row['item'] && $row['step']>0)
		{
			$shipping_fee += ceil((($unit - $row['item'])/$row['step'])) * $row['step_price'];//续费
		}
                $unit .= $compute_mode == 'by_weight' ? "公斤" : '件';
		//指定地区
		$area_sql = "select s.compute_mode,a.region_id,a.shipping_id,a.item,a.item_price,a.step,a.step_price from " . DB_PREFIX ."shipping s left join " . DB_PREFIX ."shipping_area a
	  on s.shipping_id = a.shipping_id where a.shipping_id = $shipping_id and a.region_id <> 1 limit 100";

		$row_area = $this->db->getAll($area_sql);

		if(is_array($row_area))
		{
			foreach ($row_area as $key => $val)
			{
				$arr_region = explode(',',$val['region_id']);
				if(in_array($provice,$arr_region))//地区匹配
				{

					$compute_mode = !empty($val['compute_mode']) ? $val['compute_mode'] : 'by_weight';//按重量算还是按件算
					if ($compute_mode == 'by_weight')
					{
						$shipping_desc= '首费'.$val['item'].'公斤'.$val['item_price'].'元；每增加'.$val['step'].'公斤，增加运费'.$val['step_price'].'元';
						$unit   =  $goods_weight;//计算运费的单位为重量
					}
					else
					{
						$shipping_desc= '首费'.$val['item'].'件'.$val['item_price'].'元；每增加'.$val['step'].'件，增加运费'.$val['step_price'].'元';
						$unit  = $goods_number;//计算运费的单位为数量
					}

					$shipping_fee = $val['item_price'];//首费
					if ($unit > $val['item'] && $val['step']>0)
					{
						$shipping_fee += ceil((($unit - $val['item'])/$val['step'])) * $val['step_price'];//续费
					}
                                        $unit .= $compute_mode == 'by_weight' ? "公斤" : '件';
					break;//终止纯环
				}
			}
		}

		$shipping['shipping_fee']  = $shipping_fee;//运费
		$shipping['shipping_desc'] = $shipping_desc;//运费详情
		$shipping['shipping_name'] = $row['shipping_name'];//运费详情
		$shipping['unit'] = $unit;//运费详情

		return $shipping;

	}


}

?>